package com.sxbbc.plat.controller.jz;

import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.files.RestUploadFileInfo;
import com.sxbbc.common.basics.files.UploadUtils;
import com.sxbbc.common.basics.rest.RestFactory;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.OpinionInfo;
import com.sxbbc.common.core.entity.jz.SysEssay;
import com.sxbbc.common.core.entity.jz.SysNews;
import com.sxbbc.common.core.service.*;
import com.sxbbc.common.core.utils.BasePage;
import com.sxbbc.common.core.utils.FileUtils;
import com.sxbbc.common.core.utils.MultipartFileToFile;
import com.sxbbc.plat.common.MappingUtils;
import com.sxbbc.plat.util.SystemLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;

/**
 * @author leehao
 * @description 新闻/意见
 * @date 2019-04-29 19:26
 */
@Controller
public class NewsController {

    @Autowired
    private ISysNewsService sysNewsService;
    @Autowired
    private IOpinionInfosService opinionInfosService;
    @Autowired
    private ISysEssayService sysEssayService;
    @Autowired
    private ICCustomerService customerService;
    @Autowired
    private IPushService pushService;

    /**
     * 新闻列表
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = MappingUtils.SHOW_NEWS_LIST)
    public ModelAndView showNewsList(){
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_NEWS_LIST);
        return view;
    }

    /**
     * 新闻列表数据接口
     *
     * @param page 分页
     * @return com.sxbbc.common.basics.rest.RestResponse
     * @author leehao
     */
    @RequestMapping(value = MappingUtils.QUERY_NEWS_LIST)
    @ResponseBody
    public com.sxbbc.common.basics.rest.RestResponse queryNewsList(BasePage<SysNews> page) {
        sysNewsService.page(page);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    /**
     * 新增/编辑新闻页面
     * @param id  id
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = MappingUtils.SHOW_NEWS_EDIT)
    public ModelAndView shoeNewsEdit(Integer id){
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_NEWS_EDIT);
        SysNews news = sysNewsService.getById(id);
        view.addObject("news",news);
        return view;
    }
    
    @RequestMapping(value = MappingUtils.EDIT_NEWS)
    @ResponseBody
    @SystemLog(methods = "新增/编辑新闻",module = "消息中心")
    public RestResponse editNews(@RequestParam(name = "imgUrl",required = false) MultipartFile imgUrl,
                                 SysNews news){
        /*if (news.getType() == null) {
            return GetRest.getFail("请选择发布的新闻类型");
        }*/
        if (StringUtils.isBlank(news.getTitle())) {
            return GetRest.getFail("请填写新闻标题");
        }
        if (StringUtils.isBlank(news.getDescVal())) {
            return GetRest.getFail("请填写新闻描述");
        }
        if (StringUtils.isBlank(news.getContent())) {
            return GetRest.getFail("请填写新闻内容");
        }
//        RestUploadFileInfo result = UploadUtils.getInstance().uploadFileQN(imgUrl);
//        if (result.isStatus()) {
//            news.setUrl(result.getData());
//        }else if(!result.isStatus() && imgUrl != null){
//            return GetRest.getFail("图片上传失败");
//        }

        try {
            File fe= MultipartFileToFile.multipartFileToFile(imgUrl);
            String url = FileUtils.up(fe);
            news.setUrl(url);
        }catch (Exception e){
            return GetRest.getFail("图片上传失败，请重试");
        }

        boolean save = sysNewsService.saveOrUpdate(news);
        if (!save) {
            throw new ThrowJsonException("保存新闻失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    /**
     * 删除新闻
     * @param id 新闻id
     * @author leehao
     * @return boolean
     */
    @RequestMapping(value = MappingUtils.DELETE_NEWS)
    @ResponseBody
    @SystemLog(methods = "删除新闻",module = "新闻管理")
    public RestResponse deleteNewsById(Integer id){
        boolean delete = sysNewsService.removeById(id);
        if (!delete) {
            throw new ThrowJsonException("删除新闻失败");
        }
        return GetRest.getSuccess("删除成功");
    }

    /**
     * 意见列表页面
     * @author leehao
     * @return org.springframework.web.servlet.ModelAndView
     */
    @RequestMapping(value = MappingUtils.SHOW_OPINION_LIST)
    public ModelAndView showOpinionList(){
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_OPINION_LIST);
        return view;
    }

    @RequestMapping(value = MappingUtils.QUERY_OPINION_LIST)
    @ResponseBody
    public com.sxbbc.common.basics.rest.RestResponse queryOpinionList(BasePage<OpinionInfo> page, OpinionInfo opinion) {
        opinionInfosService.queryOpinionList(page, opinion);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    /**
     * 去处理意见
     * @param id 意见id
     * @param status 1未处理2已处理
     * @author leehao
     * @return com.function.common.RestResponse
     */
    @RequestMapping(value = MappingUtils.HANDLE_OPINION_STATUS)
    @ResponseBody
    @SystemLog(methods = "处理意见",module = "消息中心")
    public RestResponse handleOpinionStatus(Integer id,Integer status){
        return opinionInfosService.handleOpinionStatus(id,status);
    }

    /**
     * 删除意见
     * @param id 意见id
     * @author leehao
     * @return boolean
     */
    @RequestMapping(value = MappingUtils.DELETE_OPINION)
    @ResponseBody
    @SystemLog(methods = "删除意见",module = "消息中心")
    public RestResponse deleteOpinion(Integer id){
        boolean delete = opinionInfosService.removeById(id);
        if (!delete) {
            throw new ThrowJsonException("删除意见失败");
        }
        return GetRest.getSuccess("删除成功");
    }

    /**
     * 查询商学院列表
     *
     * @param essay
     * @return
     */
    @RequestMapping(value = MappingUtils.SHOW_ESSAY_LIST)
    public ModelAndView showEssayList(BasePage<SysEssay> page, SysEssay essay) {
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_ESSAY_LIST);
        return view;
    }

    @RequestMapping(value = MappingUtils.QUERY_ESSAY_LIST)
    @ResponseBody
    public com.sxbbc.common.basics.rest.RestResponse queryEssayList(BasePage<SysEssay> page, SysEssay essay) {
        sysEssayService.queryEssay(page, essay);
        return RestFactory.success(page.getRecords(), page.getTotal());
    }

    /**
     * 新增/编辑商学院页面
     *
     * @param id id
     * @return ModelAndView
     */
    @RequestMapping(value = MappingUtils.SHOW_ESSAY_EDIT)
    public ModelAndView shoeEssayEdit(Integer id) {
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_ESSAY_EDIT);
        SysEssay essay = sysEssayService.getById(id);
        view.addObject("essay", essay);
        return view;
    }

    /**
     * 新增编辑资讯
     *
     * @param imgUrl 封面图
     * @param essay
     * @return
     */
    @RequestMapping(value = MappingUtils.EDIT_ESSAY)
    @ResponseBody
    @SystemLog(methods = "新增/编辑资讯",module = "消息中心")
    public RestResponse editEssay(@RequestParam(name = "coverUrlFile",required = false) MultipartFile imgUrl,
                                 @RequestParam(name = "videoUrlFile", required = false) MultipartFile videoUrl, SysEssay essay){
        if (StringUtils.isBlank(essay.getTitle())) {
            return GetRest.getFail("请填写资讯标题");
        }
        if (StringUtils.isBlank(essay.getContent())) {
            return GetRest.getFail("请填写资讯内容");
        }
        if (essay.getId() == null && imgUrl == null) {
            return GetRest.getFail("请上传封面图");
        }
        /*RestUploadFileInfo result = UploadUtils.getInstance().uploadFileQN(imgUrl);
        if (result.isStatus()) {
            essay.setCoverUrl(result.getData());
        } else if (!result.isStatus() && imgUrl != null) {
            return GetRest.getFail("图片上传失败");
        }*/

        try {
            File fe= MultipartFileToFile.multipartFileToFile(imgUrl);
            String url = FileUtils.up(fe);
            essay.setCoverUrl(url);
        }catch (Exception e){
            return GetRest.getFail("图片上传失败，请重试");
        }

        if (CalcuUtils.getInstance().isEquals(essay.getHasVideo(), StaticUtils.STATUS_YES)) {
            if (videoUrl == null) {
                return GetRest.getFail("请上传视频");
            }
            /*result = UploadUtils.getInstance().uploadFileQN(videoUrl);
            if (result.isStatus()) {
                essay.setVideoUrl(result.getData());
                essay.setHasVideo(StaticUtils.STATUS_YES);
            } else if (!result.isStatus() && videoUrl != null) {
                return GetRest.getFail("视频上传失败");
            }*/
            try {
                File fe= MultipartFileToFile.multipartFileToFile(videoUrl);
                String url = FileUtils.up(fe);
                essay.setVideoUrl(url);
                essay.setHasVideo(StaticUtils.STATUS_YES);
            }catch (Exception e){
                return GetRest.getFail("视频上传失败");
            }
        } else {
            essay.setVideoUrl("");
        }

        boolean save = sysEssayService.saveOrUpdate(essay);
        if (!save) {
            throw new ThrowJsonException("保存资讯失败");
        }
        return GetRest.getSuccess("保存成功");
    }

    /**
     * 删除资讯
     * @param id 资讯id
     * @return boolean
     */
    @RequestMapping(value = MappingUtils.DELETE_ESSAY)
    @ResponseBody
    @SystemLog(methods = "删除资讯",module = "资讯")
    public RestResponse deleteEssayById(Integer id){
        boolean delete = sysEssayService.removeById(id);
        if (!delete) {
            throw new ThrowJsonException("删除资讯失败");
        }
        return GetRest.getSuccess("删除成功");
    }



    /**
     * 站内信
     *
     * @param
     * @return
     */
    @RequestMapping(value = MappingUtils.SHOW_PUSH)
    public ModelAndView showPush() {
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_PUSH);
        return view;
    }


    /**
     * 推送用户选择-页面
     * @return
     */
    @RequestMapping(value = MappingUtils.SHOW_CUSTOMER)
    public ModelAndView showCustomerList(String content) {
        ModelAndView view = new ModelAndView(MappingUtils.SHOW_CUSTOMER);
        view.addObject("content",content);
        return view;
    }


    /**
     * 推送-站内信
     * @param cidlist 用户id集合
     * @return boolean
     */
    @RequestMapping(value = MappingUtils.HANDLE_PUSH)
    @ResponseBody
    public RestResponse handlePush(Integer[] cidlist,String pushType,String content){
        if(StringUtils.isBlank(pushType)){
            return GetRest.getFail("请选择推送类型");
        }
        if (StringUtils.isBlank(content)) {
            return GetRest.getFail("请填写推送内容");
        }
        if(pushType.equals("single")){
            if(cidlist == null || cidlist.length == 0){
                return GetRest.getFail("请选择消息接收人");
            }
            for(Integer cid:cidlist){
                //消息推送 极光推送 本版本使用token作为alias
                pushService.pushPlatMessage(pushType,cid,content);
            }
        }else if(pushType.equals("all")){
            pushService.pushPlatMessage(pushType,null,content);
        }
        return GetRest.getSuccess("推送成功");
    }
}
